package giermala.dominik.fotka.controller;

import giermala.dominik.fotka.domain.Note;
import giermala.dominik.fotka.domain.User;
import giermala.dominik.fotka.repository.FotoRepository;
import giermala.dominik.fotka.repository.UserRepository;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class NoteController {

	private static final Logger logger = LoggerFactory.getLogger(NoteController.class);

	@Autowired private FotoRepository fotoRepository;
	@Autowired private UserRepository userRepository;
	
	@RequestMapping(value="/add_note.html", method = RequestMethod.POST)
	public String addNote(@Validated @ModelAttribute Note note, BindingResult result, HttpServletRequest request) {
		
		logger.info("Note addition");
		if (result.hasErrors()) {
			for (ObjectError error : result.getAllErrors()) {
				logger.info("Error: " + error.getCode() + " - " + error.getDefaultMessage());
			}
			return "random_foto";
		}
		
		/*User user = (User)request.getSession().getAttribute("user");
		user.getNotes().add(note);
		foto.getNotes().add(note);
		note.setDate(new Date());
		note.setFoto(foto);
		note.setFotkaUser(user);
		fotoRepository.save(foto);
		userRepository.save(user);*/
		
		return "random_foto";
	}
	
}
